package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateCTypes
//@description: 创建CTypes记录
//@param: cTypes model.CTypes
//@return: err error

func CreateCTypes(cTypes model.CTypes) (err error) {
	err = global.GVA_DB.Create(&cTypes).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteCTypes
//@description: 删除CTypes记录
//@param: cTypes model.CTypes
//@return: err error

func DeleteCTypes(cTypes model.CTypes) (err error) {
	err = global.GVA_DB.Delete(&cTypes).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteCTypesByIds
//@description: 批量删除CTypes记录
//@param: ids request.IdsReq
//@return: err error

func DeleteCTypesByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.CTypes{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateCTypes
//@description: 更新CTypes记录
//@param: cTypes *model.CTypes
//@return: err error

func UpdateCTypes(cTypes model.CTypes) (err error) {
	err = global.GVA_DB.Save(&cTypes).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetCTypes
//@description: 根据id获取CTypes记录
//@param: id uint
//@return: err error, cTypes model.CTypes

func GetCTypes(id uint) (err error, cTypes model.CTypes) {
	err = global.GVA_DB.Where("id = ?", id).Preload(`CTypes`).First(&cTypes).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetCTypesInfoList
//@description: 分页获取CTypes记录
//@param: info request.CTypesSearch
//@return: err error, list interface{}, total int64

func GetCTypesInfoList(info request.CTypesSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.CTypes{})
    var cTypess []model.CTypes
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.Name != "" {
        db = db.Where("`name` LIKE ?","%"+ info.Name+"%")
    }
    if info.Pid != 0 {
        db = db.Where("`pid` = ?",info.Pid)
    }else{
		db = db.Where("`pid` = ?",0)
	}
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Preload(`CTypes`).Find(&cTypess).Error
	return err, cTypess, total
}


func GetCTypesInfoLists(info request.CTypesSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&model.CTypes{})
	var cTypess []model.CTypes
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.Name != "" {
		db = db.Where("`name` LIKE ?","%"+ info.Name+"%")
	}
	if info.Pid != 0 {
		db = db.Where("`pid` = ?",info.Pid)
	}
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Preload(`CTypes`).Find(&cTypess).Error
	return err, cTypess, total
}